package com.link_with_health.service;

import com.link_with_health.exception.BaseException;
import com.link_with_health.exception.ModifyDatabaseFailedException;
import com.link_with_health.mapper.BatchOperationMapper;
import com.link_with_health.model.BatchModel;
import com.link_with_health.model.TableName;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service("batchOperationService")
public class BatchOperationService extends BaseService {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Resource
    BatchOperationMapper batchOperationMapper;


    public void batchChangeStatus(List<BatchModel> models) throws BaseException {
        String tableName = models.get(0).getTableName();
        int result = 0;
        try {
            result = batchOperationMapper.batchUpdate(models);
        } catch(Exception e){
            logger.error("batch update "+tableName +" failed",e);
        }
        if (result == 0)
            throw new ModifyDatabaseFailedException("BATCH UPDATE "+tableName);

    }

    public void batchUpdateLikeDelete(List<BatchModel> models) throws Exception{
        String tableName = models.get(0).getTableName();

        int result = 0;
        try {
            result = batchOperationMapper.batchUpdateLikeDelete(models);
        } catch(Exception e){
            logger.error("batch update-DELETE "+tableName +" failed",e);
        }
        if (result == 0)
            throw new ModifyDatabaseFailedException("BATCH UPDATE-DELETE "+tableName);
    }
}
